<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="巡护人姓名" prop="name" label-width="100px">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入巡护人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="beginTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.beginTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['frontierCommand:patrolRecord:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['frontierCommand:patrolRecord:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['frontierCommand:patrolRecord:remove']"
        >批量删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['frontierCommand:patrolRecord:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="patrolRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="巡护人姓名" align="center" prop="name" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="巡护人员账号" align="center" prop="patrolSysName"/>
      <el-table-column label="巡护任务" align="center" prop="missionName"/>
      <el-table-column label="开始时间" align="center" prop="beginTime" width="180">
        <!--        <template slot-scope="scope">-->
        <!--          <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <!--        <template slot-scope="scope">-->
        <!--          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column label="巡护距离" align="center" prop="distance" width="180">
        <template slot-scope="scope">
          <span>{{(scope.row.distance/1000).toFixed(2) }}{{'公里'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['frontierCommand:patrolRecord:edit']"-->
<!--          >修改-->
<!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['frontierCommand:patrolRecord:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改巡护记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="巡护人ID" prop="appUserId">
          <el-input v-model="form.appUserId" placeholder="请选择巡护人" :disabled="false"  style="width:230px"/>
          <el-button type="primary"  @click="openPatrol"  style="margin-left:10px;width:130px">请选择巡护人员</el-button>
        </el-form-item>
        <el-form-item label="开始时间" prop="beginTime">
          <el-date-picker clearable size="small"
                          v-model="form.beginTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable size="small"
                          v-model="form.endTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="patrolTitle" :visible.sync="patrolOpen" width="800px" append-to-body>
      <el-form :model="queryPatrolParams" ref="queryPatrolForm" :inline="true" v-show="showSearchPatrol"
               label-width="68px">
        <el-form-item label="登录名" prop="equipmentName">
          <el-input
            v-model="queryPatrolParams.userName"
            placeholder="请输入登录名"
            clearable
            size="small"
            @keyup.enter.native="handleQueryPatrol"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryPatrol">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetPatrolQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="patrolList" @selection-change="handleSelectionChangeTerminal">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="用户id" align="center" prop="userId"/>
        <el-table-column label="登录名" align="center" prop="userName"/>
        <el-table-column label="手机号" align="center" prop="phonenumber"/>
      </el-table>
      <pagination
        v-show="totalPatrol>0"
        :total="totalPatrol"
        :page.sync="queryPatrolParams.pageNum"
        :limit.sync="queryPatrolParams.pageSize"
        @pagination="getPatrolList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okPatrol">确 定</el-button>
        <el-button @click="cancelPatrol">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listAppUser} from "@/api/basicInformation/appUser";
  import {
    listPatrolRecord,
    getPatrolRecord,
    delPatrolRecord,
    addPatrolRecord,
    updatePatrolRecord,
    exportPatrolRecord
  } from '@/api/frontierCommand/patrolRecord'

  export default {
    name: 'PatrolRecord',
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 巡护记录表格数据
        patrolRecordList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 选择人员弹出层标题
        patrolTitle: '选择巡护人员',
        // 是否显示选择人员弹出层
        patrolOpen: false,
        // 显示搜索条件
        showSearchPatrol: true,
        totalPatrol: 0,
        //巡护人员集合
        patrolList: [],
        //搜索巡护人员用到字段
        idsPatrol: [],
        patrolNames: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          beginTime: undefined,
          endTime: undefined,
          orderByColumn: 'beginTime',
          isAsc: 'desc'
        },
        // 查询巡护人员                                                                                                                                                                                                                                        参数
        queryPatrolParams: {
          pageNum: 1,
          pageSize: 10,
          userName: undefined,
          userType: 2
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询巡护记录列表 */
      getList() {
        this.loading = true
        listPatrolRecord(this.queryParams).then(response => {
          this.patrolRecordList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      /** 获取巡护人员列表 */
      getPatrolList() {
        this.loading = true
        listAppUser(this.queryPatrolParams).then(response => {
          this.patrolList = response.rows
          this.totalPatrol = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          appUserId: undefined,
          beginTime: undefined,
          endTime: undefined,
          delFlag: undefined,
          createById: undefined,
          createBy: undefined,
          createTime: undefined,
          updateById: undefined,
          updateBy: undefined,
          updateTime: undefined
        }
        this.resetForm('form')
      },
      /** 巡护人员搜索按钮操作 */
      handleQueryPatrol() {
        this.queryPatrolParams.pageNum = 1
        this.getPatrolList()
      },
      /** 巡护人员重置按钮操作 */
      resetPatrolQuery() {
        this.resetForm('form')
        this.handleQueryTerminal()
      },
      /** 打开选择巡护人员*/
      openPatrol(){
        this.patrolOpen = true
        this.handleQueryPatrol()
      },
      // 多选框选中数据
      handleSelectionChangeTerminal(selection) {
        this.idsPatrol = selection.map(item => item.userId)
        this.patrolNames = selection.map(item => item.userName)
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.names = selection.map(item => item.missionName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加巡护记录'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getPatrolRecord(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改巡护记录'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updatePatrolRecord(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addPatrolRecord(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        const names = row.missionName || this.names
        this.$confirm('是否确认删除巡护任务名称为"' + names + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delPatrolRecord(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有巡护记录数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportPatrolRecord(queryParams)
        }).then(response => {
          this.download(response.msg)
        })
      },
      okPatrol() {
        debugger
        if (this.idsPatrol.length > 1) {
          this.msgInfo('只能选择一个巡护人员')
        } else if (this.idsPatrol.length === 1) {
          this.form.appUserId = this.idsPatrol[0]
          // this.form.patrolName = this.patrolNames[0]
          this.patrolOpen = false
        } else {
          this.msgInfo('请选择巡护人员')
        }
      },
      cancelPatrol() {
        this.patrolOpen = false
      }
    }
  }
</script>
